//
//  CategoryViewController.m
//  海角音乐
//
//  Created by dllo on 15/8/17.
//  Copyright (c) 2015年 杨元石. All rights reserved.
//

#import "CategoryViewController.h"

@interface CategoryViewController ()

@end

@implementation CategoryViewController

- (void)dealloc
{
    [_collectionView release];
    [_CateModelArr release];
    [_languageArr release];
    [_flowlayout release];
    [_specialArr release];
    [_customArr release];
    [_wusunArr release];
    [_placeArr release];
    [_mindArr release];
    [_hotArr release];
    [_ageArr release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated
{
    BarPlayer = [DanLiPlayer shareHandleDanLiPlayer:CGRectZero];
    [self.view addSubview:BarPlayer];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    

    self.TitleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200*ADJUSTWIDTH, 30*ADJUSTWIDTH)];
    self.TitleView.textColor = [UIColor whiteColor];
    self.TitleView.textAlignment = NSTextAlignmentCenter;
    self.TitleView.text = @"分类";
    self.navigationItem.titleView = self.TitleView;
    
    
    self.CateModelArr = [[NSMutableArray alloc] init];    
    self.hotArr = [[NSMutableArray alloc] init];
    self.wusunArr = [[NSMutableArray alloc] init];
    self.placeArr = [[NSMutableArray alloc] init];
    self.languageArr = [[NSMutableArray alloc] init];
    self.mindArr = [[NSMutableArray alloc] init];
    self.customArr = [[NSMutableArray alloc] init];
    self.specialArr = [[NSMutableArray alloc] init];
    self.ageArr = [[NSMutableArray alloc] init];
    
    
    CGFloat width = SCREEN_WIDTH*0.293333; // 110
    CGFloat height = SCREEN_WIDTH*0.32; // 120
    
    
    [self setAutomaticallyAdjustsScrollViewInsets:YES];
    
    
    self.flowlayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowlayout.itemSize = CGSizeMake(width, height);
    self.flowlayout.minimumLineSpacing = 1;
    self.flowlayout.minimumInteritemSpacing = 1;
    self.flowlayout.sectionInset = UIEdgeInsetsMake(5,10,10,5);
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64-50) collectionViewLayout:self.flowlayout];
    self.collectionView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_collectionView];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    [self.collectionView registerClass:[CollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LLLLLL"];
    [self.collectionView registerClass:[CateCollectionViewCell class] forCellWithReuseIdentifier:@"reuseld"];
    
    HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.removeFromSuperViewOnHide = YES;
    HUD.labelText = @"loading...";
    [self.view addSubview:HUD];
    HUD.mode = MBProgressHUDModeIndeterminate;    
    [self getDataForCategory];
    
    
    
    [_CateModelArr release];
    [_languageArr release];
    [_specialArr release];
    [_customArr release];
    [_wusunArr release];
    [_placeArr release];
    [_mindArr release];
    [_ageArr release];
    [_hotArr release];
    
    // Do any additional setup after loading the view.
}


- (void)getDataForCategory
{
    [AFN urlString:CategoryMusicUrl getAFNdata:^(id responseObject) {
        
        NSArray *dataArr = [responseObject objectForKey:@"data"];
        for (NSDictionary *dic in dataArr) {
            
            CategoryModel *model = [[CategoryModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.CateModelArr addObject:model];
            [model release];
            
        }
        
        for (CategoryModel *model in self.CateModelArr) {
            
            if ([model.parentname isEqualToString:@"热门"]) {
                
                [self.hotArr addObject:model];
                
            } else if ([model.parentname isEqualToString:@"无损"]) {
                
                [self.wusunArr addObject:model];
            } else if ([model.parentname isEqualToString:@"场景"]) {
                
                [self.placeArr addObject:model];
            } else if ([model.parentname isEqualToString:@"语言"]) {
                
                [self.languageArr addObject:model];
            } else if ([model.parentname isEqualToString:@"心情"]) {
                
                [self.mindArr addObject:model];
            } else if ([model.parentname isEqualToString:@"风格"]) {
                
                [self.customArr addObject:model];
            } else if ([model.parentname isEqualToString:@"特色"]) {
                
                [self.specialArr addObject:model];
            } else if ([model.parentname isEqualToString:@"年代"]) {
                
                [self.ageArr addObject:model];
            }
        }
        HUD.hidden = YES;
        [self.collectionView reloadData];
    }];
    
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return 8;
    }else if (section == 1)
    {
        return 6;
    }else if (section == 2)
    {
        return 13;
    }else if (section == 3)
    {
        return 7;
    }else if (section == 4)
    {
        return 11;
    }else if (section == 5)
    {
        return 12;
    }else if (section == 6)
    {
        return 13;
    }else
    {
        return 3;
    }
    
    
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 8;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        
        static NSString *reuseID = @"reuseld";
        CateCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseID forIndexPath:indexPath];
        
        if (_hotArr.count > indexPath.row) {
            CategoryModel *model = [_hotArr objectAtIndex:indexPath.row];
            [cell getModel:model];
        }
        return cell;
        
    } else if (indexPath.section == 1) {
        
        static NSString *reuseID = @"reuseld";
        CateCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseID forIndexPath:indexPath];
        
        if (_wusunArr.count > indexPath.row) {
            CategoryModel *model = [_wusunArr objectAtIndex:indexPath.row];
            [cell getModel:model];
        }
        return cell;
    } else if (indexPath.section == 2) {
        
        static NSString *reuseID = @"reuseld";
        CateCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseID forIndexPath:indexPath];
        if (_placeArr.count > indexPath.row) {
            CategoryModel *model = [_placeArr objectAtIndex:indexPath.row];
            [cell getModel:model];
        }     
        return cell;
        
    } else if (indexPath.section == 3) {
        
        static NSString *reuseID = @"reuseld";
        CateCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseID forIndexPath:indexPath];
        
        if (_languageArr.count > indexPath.row) {
            CategoryModel *model = [_languageArr objectAtIndex:indexPath.row];
            [cell getModel:model];
        }
        return cell;
    } else if (indexPath.section == 4) {
        
        static NSString *reuseID = @"reuseld";
        CateCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseID forIndexPath:indexPath];
        
        if (_mindArr.count > indexPath.row) {
            CategoryModel *model = [_mindArr objectAtIndex:indexPath.row];
            [cell getModel:model];
        }
        return cell;
    } else if (indexPath.section == 5) {
        
        static NSString *reuseID = @"reuseld";
        CateCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseID forIndexPath:indexPath];
        
        
        if (_customArr.count > indexPath.row) {
            CategoryModel *model = [_customArr objectAtIndex:indexPath.row];
            [cell getModel:model];
        }
        
        return cell;
    } else if (indexPath.section == 6) {
        
        static NSString *reuseID = @"reuseld";
        CateCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseID forIndexPath:indexPath];
        
        if (_specialArr.count > indexPath.row) {
            CategoryModel *model = [_specialArr objectAtIndex:indexPath.row];
            [cell getModel:model];
        }
        return cell;
    } else  {
        
        static NSString *reuseID = @"reuseld";
        CateCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseID forIndexPath:indexPath];
        
        if (_ageArr.count > indexPath.row) {
            CategoryModel *model = [_ageArr objectAtIndex:indexPath.row];
            [cell getModel:model];
        }
        return cell;
    }

    
    
    

    
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
    if (kind == UICollectionElementKindSectionHeader) {
        
        CollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"LLLLLL" forIndexPath:indexPath];
        
        header.backgroundColor = [UIColor clearColor];

        if (indexPath.section == 0) {
            header.diLabel.text = @"热门";
            return header;
        }else if (indexPath.section == 1)
        {
            header.diLabel.text = @"无损";
            return header;
        }else if (indexPath.section == 2)
        {
            header.diLabel.text = @"场景";
            return header;
        }else if (indexPath.section == 3)
        {
            header.diLabel.text = @"语言";
            return header;
        }else if (indexPath.section == 4)
        {
            header.diLabel.text = @"心情";
            return header;
        }else if (indexPath.section == 5)
        {
            header.diLabel.text = @"风格";
            return header;
        }else if (indexPath.row == 6)
        {
            header.diLabel.text = @"特色";
        }
        else {
            header.diLabel.text = @"年代";
            return header;
        }
        return nil;
    }
    
    return nil;
}


-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    CGFloat HH = 25*ADJUSTWIDTH; // 20
    
    CGSize size={0,HH};
    return size;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
   
     CateDetailViewController *cateVc = [[CateDetailViewController alloc] init];
    
    if (indexPath.section == 0) {
        
        CategoryModel *model = [_hotArr objectAtIndex:indexPath.row];
        cateVc.model = model;
    } else if (indexPath.section == 1) {
        
        CategoryModel *model = [_wusunArr objectAtIndex:indexPath.row];
        cateVc.model = model;
    } else if (indexPath.section == 2) {
        
        CategoryModel *model = [_placeArr objectAtIndex:indexPath.row];
        cateVc.model = model;
    } else if (indexPath.section == 3) {
        
        CategoryModel *model = [_languageArr objectAtIndex:indexPath.row];
        cateVc.model = model;
    } else if (indexPath.section == 4) {
        
        CategoryModel *model = [_mindArr objectAtIndex:indexPath.row];
        cateVc.model = model;
    } else if (indexPath.section == 5) {
        
        CategoryModel *model = [_customArr objectAtIndex:indexPath.row];
        cateVc.model = model;
    } else if (indexPath.section == 6) {
        
        CategoryModel *model = [_specialArr objectAtIndex:indexPath.row];
        cateVc.model = model;
    } else {
        CategoryModel *model = [_ageArr objectAtIndex:indexPath.row];
        cateVc.model = model;
    }    
  
    [self.navigationController pushViewController:cateVc animated:YES];
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
